#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@version: ??
@author: happiness
@license: Apache Licence 
@contact: happiness_ws@163.com
@site: 
@software: PyCharm
@file: nn_net.py
@time: 2017/11/8 9:28
"""

import tensorflow as tf
import numpy as np

# 创建模拟数据，3x3x3数组
M = np.array(
    [[[1, 0, 2], [-1, 0, -1], [2, 1, 1]], [[2, 1, 2], [1, 2, 0], [-1, -1, -1]], [[0, 0, 0], [1, -1, 1], [1, 0, 0, ]]])

print("M shape is ", M.shape)

# 定义卷积过滤器
filter_weights = tf.get_variable("weights", shape=[2, 2, 3, 3], dtype=tf.float32,
                                 initializer=tf.truncated_normal_initializer(stddev=0.1))

biase = tf.get_variable('biase', shape=[3], initializer=tf.constant_initializer([0.5]))

M = np.asanyarray(M, dtype="float32")
M = M.reshape([1, 3, 3, 3])

# 创建输入
x = tf.placeholder(dtype=tf.float32, shape=[1, None, None, None], name="x-input")
# 创建卷积层
conv = tf.nn.conv2d(x, filter=filter_weights, strides=[1, 1, 1, 1], padding="SAME")
bias = tf.nn.bias_add(conv, biase)
# 创建池化层,过滤器尺寸一四维数据必须为1，步长一四维数据必须为1
pool = tf.nn.avg_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    conv_M = sess.run(bias, feed_dict={x: M})
    pool_M = sess.run(pool, feed_dict={x: conv_M})

    print("conv_M:", conv_M)
    print("pool_M", pool_M)
if __name__ == '__main__':
    pass
